import gtk

class Dialogs:
    def open(self):
        fileDialog = gtk.FileChooserDialog(\
                        title='Open Worksheet',
                        action=gtk.FILE_CHOOSER_ACTION_OPEN,                                            
                        buttons=(   gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
                                    gtk.STOCK_OPEN, gtk.RESPONSE_OK))
        response = fileDialog.run()
        if response == gtk.RESPONSE_OK:
            path = fileDialog.get_filename()
        else:
            path = None
        fileDialog.destroy()
        return path
    
    def saveAs(self, worksheet):
        fileDialog = gtk.FileChooserDialog(\
                        title='Save Worksheet: '+worksheet.caption,
                        action=gtk.FILE_CHOOSER_ACTION_SAVE,                                            
                        buttons=(   gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
                                    gtk.STOCK_SAVE, gtk.RESPONSE_OK))
        response = fileDialog.run()
        path = fileDialog.get_filename()
        fileDialog.destroy()
        if response != gtk.RESPONSE_OK: return False
        else: return path
    
    def saveSingle(self, worksheet):
        text = 'Do you want to save changes in ' + worksheet.caption + '?'
        dialog = gtk.MessageDialog(
                          type=gtk.MESSAGE_QUESTION,
                          buttons=gtk.BUTTONS_YES_NO,
                          message_format=text)
        response = dialog.run()
        dialog.destroy()

        if response==gtk.RESPONSE_NO:
            return False
        elif response==gtk.RESPONSE_YES:
            return True
        return None
    
    def saveMulti(self, worksheets):
        vbox = gtk.VBox()
        vbox.show()
        
        scroll = gtk.ScrolledWindow()
        scroll.set_policy(gtk.POLICY_AUTOMATIC,gtk.POLICY_AUTOMATIC)
        scroll.show()
        scroll.add_with_viewport(vbox)
        
        header = gtk.Label('asf')
        header.show()
        placeholder = gtk.Label('')
        placeholder.show()
        
        hbox = gtk.HBox()
        hbox.show()
        hbox.pack_end(placeholder)
        hbox.pack_end(header, expand=False)
        
        vbox1 = gtk.VBox()
        vbox1.show()
        vbox1.pack_end(scroll)
        vbox1.pack_end(hbox, expand=False)
        
        
        dialog = gtk.Dialog('Save')
        dialog.add_button('close without saving', gtk.RESPONSE_CLOSE)
        dialog.add_button('cancel', gtk.RESPONSE_CANCEL)
        dialog.add_button('save', gtk.RESPONSE_YES)
        dialog.vbox.pack_end(vbox1)
        
        vbox.pack_start(gtk.Label())
        
        checkButton = {}
        for i in worksheets:
            checkButton[i] = gtk.CheckButton(i.caption)
            checkButton[i].set_active(True)
            checkButton[i].show()
            vbox.pack_start(checkButton[i], expand=False)
        
        response = dialog.run()
        dialog.destroy()
        
        if response==gtk.RESPONSE_CLOSE:
            return []
        elif response==gtk.RESPONSE_YES:
            buf = []
            for i in worksheets:
                if checkButton[i].get_active():
                    buf.append(i)
            return buf
        return None
